package com.example.project7;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

public class HomeFragment extends Fragment {

    private TextView tvRecommendation;
    private SharedViewModel sharedViewModel;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        tvRecommendation = view.findViewById(R.id.tv_recommendation);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // 获取与 Activity 关联的 ViewModel 实例
        sharedViewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);

        // 观察兴趣数据的变化
        sharedViewModel.getCurrentInterest().observe(getViewLifecycleOwner(), interest -> {
            // 当数据变化时，这个回调会自动执行
            tvRecommendation.setText("根据您的兴趣【" + interest + "】，推荐内容如下");
        });
    }
}